package cn.wolfcode.rbac.springsecurity.detail;

import cn.wolfcode.rbac.domain.Employee;
import lombok.Getter;
import lombok.Setter;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.CollectionUtils;

import java.util.Collection;
import java.util.Collections;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 自定义UserDetails封装用户信息
 */
@Getter
@Setter
public class EmployeeDetails implements UserDetails {

    private Employee employee;
    private Set<SimpleGrantedAuthority> expressions;

    public EmployeeDetails(Employee employee,Set<String> expressions) {
        this.employee = employee;
        if(!CollectionUtils.isEmpty(expressions)){
           this.expressions = expressions.stream().map(SimpleGrantedAuthority::new).collect(Collectors.toSet());
        }
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return expressions;
    }

    @Override
    public String getPassword() {
        return employee.getPassword();
    }

    @Override
    public String getUsername() {
        return employee.getName();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
